﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using JuniorProject.Models;

namespace JuniorProject.Controllers
{
    public class ApplicationController : Controller
    {
        private OIT_Junior_ProjectEntities db = new OIT_Junior_ProjectEntities();

        // GET: /Application/

        public ActionResult Index()
        {
            return View();
        }

        #region BASIC APPLICATION
        // GET: /Application/Application
        [AllowAnonymous]
        public ActionResult Application(string returnUrl, int? JobID, int? JobPostingID)
        {
            ViewBag.ReturnUrl = returnUrl;
            ViewBag.JobID = JobID;
            ViewBag.JobPostingID = JobPostingID;
            return View();
        }

        // POST: /Application/Application
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Application(BasicApplicationModel model, string returnUrl, int? JobID, int? JobPostingID)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    bool saved = AddBasicApplication(model);

                    if (saved)
                    {
                        return RedirectToLocal(returnUrl, JobID, JobPostingID);
                    }
                    else
                    {
                        return RedirectToAction("Application", "Application", returnUrl);
                    }
                }
                else
                {
                    return RedirectToAction("Application", "Application", returnUrl);
                }
                    
            }
            catch (Exception e)
            {
                ModelState.AddModelError("", e.Message);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
        #endregion

        #region DETAILED APPLICATION
        // GET: /Application/ApplicationDetails
        public ActionResult ApplicationDetails(int? JobID, int? JobPostingID)
        {
            if (String.IsNullOrEmpty(User.Identity.Name))
            {
                return RedirectToAction("Login", "Account", new { returnUrl = "Application/ApplicationDetails", JobID, JobPostingID });
            }

            ViewBag.JobID = JobID;
            ViewBag.JobPostingID = JobPostingID;
            return View();
        }

        // POST: /Application/ApplicationDetails
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult ApplicationDetails(DetailedApplicationModel model, string returnUrl, int? JobID, int? JobPostingID)
        {
            int applicationID = 0;
            ViewBag.JobID = JobID;
            ViewBag.JobPostingID = JobPostingID;
            try
            {
                if (ModelState.IsValid)
                {
                    applicationID = AddDetailedApplication(model, JobPostingID);
                    ViewBag.applicationID = applicationID;

                    if (applicationID != 0)
                    {
                        return RedirectToAction("Questionnaire", "Application", new { JobID , applicationID });
                    }
                }
                else
                {
                    return RedirectToAction("Application", "Application", returnUrl);
                }

            }
            catch (Exception e)
            {
                ModelState.AddModelError("", e.Message);
            }

            //If we got this far, something failed, redisplay form

            return View();
        }
        #endregion

        #region QUESTIONNAIRE
        // GET: /Application/Questionnaire
        public ActionResult Questionnaire(int? JobID, int? applicationID)
        {
            List<JobToQuestions> tempJobToQuestion =
                (from q in db.JobToQuestions where q.JobID == JobID select q).ToList();
            QuestionListModel questionModels = new QuestionListModel();
            if (tempJobToQuestion != null)
            {
                questionModels.Questions = tempJobToQuestion.ConvertAll(new Converter<JobToQuestions,
                                                ScreeningQuestionModel>(DbScnQuestionsToModel));
            }

            ViewBag.applicationID = applicationID;
            return View(questionModels);
        }

        // POST: /Application/Questionnaire
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Questionnaire(QuestionListModel model, int? applicationID)
        {
            try
            {
                if (ModelState.IsValid)
                {   
                    //Create Temporary List to Hold Model Items
                    QuestionListModel tempModel = new QuestionListModel(model);

                    //Post responses for previously created application
                    foreach (var q in tempModel.Questions)
                    {
                        var qId = q.ID;
                        var selectedAnswer = q.SelectedAnswer;

                        // Map the Questionnaire to Each Application
                        UserResponses responses = new UserResponses();
                        responses.UserID = db.Users.Single(x => x.UserName == User.Identity.Name).ID;
                        responses.QuestionID = q.ID;
                        responses.AnswerID = Convert.ToInt32(q.SelectedAnswer);
                        responses.ApplicationID = (int)applicationID;
                        responses.InsertDate = DateTime.Now;
                        responses.StatusID = 28;
                        db.UserResponses.Add(responses);
                        db.SaveChanges();
                    }

                    //Add Application for each additional job posting
                    int tempJobPostingID = db.Applications.Find(applicationID).JobPostingID;
                    int tempJobID = db.Applications.Find(applicationID).JobPostings.JobID;
                    int tempLocationID = db.Applications.Find(applicationID).JobPostings.LocationID;

                    List<JobPostings> tempPostings = (from x in db.JobPostings
                                                      where
                                                          (x.JobID == tempJobID
                                                          && x.LocationID == tempLocationID
                                                          && x.ID != tempJobPostingID
                                                          && x.StatusID == 19)
                                                      select x).ToList();
                    Applications tempApp = db.Applications.Find(applicationID); 

                    foreach (var item in tempPostings)
                    {
                        Applications application = new Applications();

                        application.AddressID = tempApp.AddressID;
                        application.InsertDate = tempApp.InsertDate;
                        application.JobPostingID = item.ID;
                        application.ModifiedDate = System.DateTime.Now;
                        application.PhoneNumberID1 = tempApp.PhoneNumberID1;
                        application.PhoneNumberID2 = tempApp.PhoneNumberID2;
                        application.ReferenceID1 = tempApp.ReferenceID1;
                        application.ReferenceID2 = tempApp.ReferenceID2;
                        application.ReferenceID3 = tempApp.ReferenceID3;
                        application.Salary = tempApp.Salary;
                        application.UserID = tempApp.UserID;
                        application.StatusID = 23;

                        db.Applications.Add(application);
                        db.SaveChanges();
                        
                        foreach (var q in tempModel.Questions)
                        {
                            var qId = q.ID;
                            var selectedAnswer = q.SelectedAnswer;

                            // Map the Questionnaire to Each Application
                            UserResponses responses = new UserResponses();
                            responses.UserID = db.Users.Single(x => x.UserName == User.Identity.Name).ID;
                            responses.QuestionID = q.ID;
                            responses.AnswerID = Convert.ToInt32(q.SelectedAnswer);
                            responses.ApplicationID = application.ID;
                            responses.InsertDate = DateTime.Now;
                            responses.StatusID = 28;
                            db.UserResponses.Add(responses);
                            db.SaveChanges();
                        }
                    }

                    return RedirectToAction("Success", "Application");
                }
            }
            catch (Exception e)
            {
                ModelState.AddModelError("", e.Message);
            }
            // If we got this far, something failed, redisplay form
            return View(model);
        }
        #endregion

        #region VERIFY INFO
        // GET: /Application/Verify
        public ActionResult Verify(string returnUrl, int? JobID, int? JobPostingID)
        {
            int tempInt = 0;
            BasicApplicationModel appModel = new BasicApplicationModel();
            var user = db.Users.Single(x => x.UserName == User.Identity.Name);

            var app = db.BasicApplicationInfo.FirstOrDefault(b => b.UserID == user.ID);
            if (app != null)
            {
                appModel = BasicApplicationInfoToApplicationModel(app);
                tempInt = app.ID;
            }

            ViewBag.BasicApplicationID = tempInt;
            ViewBag.ReturnUrl = returnUrl;
            ViewBag.JobID = JobID;
            ViewBag.JobPostingID = JobPostingID;
            return View(appModel);
        }
        
        // POST: /Application/Verify
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Verify(BasicApplicationModel model, string returnUrl, int? JobID, int? BasicApplicationID, int? JobPostingID)
        {
            try
            {

                if (ModelState.IsValid)
                {
                    bool saved = false;
                    if (BasicApplicationID > 0)
                    {
                        saved = UpdateBasicApplication(model, BasicApplicationID);
                    }
                    else
                    {
                        saved = AddBasicApplication(model);
                    }

                    if (saved)
                    {
                        return RedirectToLocal(returnUrl, JobID, JobPostingID);
                    }
                    else
                    {
                        return RedirectToAction("Verify", "Application", returnUrl);
                    }
                }
                else
                {
                    return RedirectToAction("Verify", "Application", returnUrl);
                }

            }
            catch (Exception e)
            {
                ModelState.AddModelError("", e.Message);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
        #endregion

        // GET: /Application/Success
        public ActionResult Success(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            return View("success");
        }

        #region HELPERS
        private ActionResult RedirectToLocal(string returnUrl, int? JobID, int? JobPostingID)
        {
            String[] tempArray = returnUrl.Split('/');
            return RedirectToAction(tempArray[1], tempArray[0], new { JobID, JobPostingID });
        }

        private ScreeningQuestionModel DbScnQuestionsToModel(JobToQuestions db1)
        {
            List<QuestionToAnswer> tempQuestionToAnswer =
                (from q in db.QuestionToAnswer
                 where q.QuestionID == db1.QuestionID
                 select q).ToList();

            List<AnswerModel> answerModels = new List<AnswerModel>();
            if (tempQuestionToAnswer != null)
            {
                answerModels = tempQuestionToAnswer.ConvertAll(new Converter<QuestionToAnswer, AnswerModel>(DbScnQuestionsToAnsModel));
            }

            return new ScreeningQuestionModel(db1.QuestionID, db1.Questions.Question, answerModels);
        }
        private AnswerModel DbScnQuestionsToAnsModel(QuestionToAnswer db1)
        {
            return new AnswerModel(db1.AnswerID, db1.Answers.Answer);
        }

        private bool AddBasicApplication(BasicApplicationModel model)
        {
            bool Return = false;
            try
            {
                EmploymentHistory empHistory1 = new EmploymentHistory();
                empHistory1.Name = model.EmployerName1;
                empHistory1.Position = model.Position1;
                empHistory1.StartDate = model.StartDate1;
                empHistory1.EndDate = model.EndDate1;
                empHistory1.Address1 = model.Address11;
                empHistory1.Address2 = model.Address21;
                empHistory1.City = model.City1;
                empHistory1.State = model.State1;
                empHistory1.Zip = model.Zip1;
                empHistory1.ManagerName = model.ManagerName1;
                empHistory1.Phone = model.ManagerPhone1;
                empHistory1.StartSalary = model.StartSalary1;
                empHistory1.EndSalary = model.EndSalary1;
                empHistory1.ReasonForLeaving = model.ReasonForLeaving1;
                empHistory1.Responsibilities = model.Responsibilities1;
                empHistory1.MayWeContact = Convert.ToByte(model.MayWeContact1);
                empHistory1.InsertDate = DateTime.Now;
                empHistory1.StatusID = 26;
                db.EmploymentHistory.Add(empHistory1);
                db.SaveChanges();

                Education edHistory1 = new Education();
                edHistory1.SchoolName = model.SchoolName1;
                edHistory1.Address1 = model.SchoolAddress11;
                edHistory1.Address2 = model.SchoolAddress21;
                edHistory1.City = model.SchoolCity1;
                edHistory1.State = model.SchoolState1;
                edHistory1.Zip = model.SchoolZip1;
                edHistory1.YearStart = model.YearStart1;
                edHistory1.YearEnd = model.YearEnd1;
                edHistory1.Graduated = Convert.ToByte(model.Graduated1);
                edHistory1.InsertDate = DateTime.Now;
                edHistory1.StatusID = 26;
                db.Education.Add(edHistory1);
                db.SaveChanges();

                BasicApplicationInfo application = new BasicApplicationInfo();

                application.AltName = model.AltName;
                application.Days = Convert.ToByte(model.Days);
                application.EducationID1 = edHistory1.ID;
                application.EmploymentHistoryID1 = empHistory1.ID;
                application.Evenings = Convert.ToByte(model.Evenings);
                application.FullTime = Convert.ToByte(model.FullTime);
                application.MonStart = model.MonStart;
                application.MonStop = model.MonStop;
                application.TuesStart = model.TuesStart;
                application.TuesStop = model.TuesStop;
                application.WedStart = model.WedStart;
                application.WedStop = model.WedStop;
                application.ThurStart = model.ThurStart;
                application.ThurStop = model.ThurStop;
                application.FriStart = model.FriStart;
                application.FriStop = model.FriStop;
                application.SatStart = model.SatStart;
                application.SatStop = model.SatStop;
                application.SunStart = model.SunStart;
                application.SunStop = model.SunStop;
                application.SSN = model.SSN;
                application.UserID = db.Users.Single(x => x.UserName == User.Identity.Name).ID;
                application.StatusID = 26;
                application.InsertDate = DateTime.Now;
                db.BasicApplicationInfo.Add(application);
                db.SaveChanges();
                Return = true;
            }
            catch (Exception ex)
            {

            }

            return Return;
        }
        private int AddDetailedApplication(DetailedApplicationModel model, int? jobPostingID)
        {
            int appID = 0;

            if (jobPostingID != null)
            {
                try
                {   
                    int tempUserID = db.Users.Single(x => x.UserName == User.Identity.Name).ID;
                    var tempAddress = db.Addresses.FirstOrDefault(a => a.UserID == tempUserID);

                    if (tempAddress == null)     //ADDRESS DOESN'T EXIST - CREATE NEW ADDRESS ENTRY
                    {
                        Addresses address = new Addresses();
                        address.Address1 = model.Address1;
                        address.Address2 = model.Address2;
                        address.City = model.City;
                        address.InsertDate = DateTime.Now;
                        address.State = model.State;
                        address.StatusID = 26;
                        address.UserID = tempUserID;
                        address.Zip = model.Zip;
                        db.Addresses.Add(address);
                        db.SaveChanges();
                    }
                    else//ENTRY EXISTS - UPDATE EXISTING ADDRESS ENTRY
                    {
                        Addresses address = db.Addresses.Find(tempAddress.ID);
                        address.Address1 = model.Address1;
                        address.Address2 = model.Address2;
                        address.City = model.City;
                        address.InsertDate = DateTime.Now;
                        address.State = model.State;
                        address.StatusID = 26;
                        address.UserID = tempUserID;
                        address.Zip = model.Zip;
                        db.Addresses.Add(address);
                        db.SaveChanges();
                    }

                    PhoneNumbers phoneNumber1 = new PhoneNumbers();
                    phoneNumber1.InsertDate = DateTime.Now;
                    phoneNumber1.Number = model.ApplicantPhoneNumber1;
                    phoneNumber1.StatusID = 26;
                    phoneNumber1.UserID = db.Users.Single(x => x.UserName == User.Identity.Name).ID;
                    db.PhoneNumbers.Add(phoneNumber1);
                    db.SaveChanges();

                    PhoneNumbers phoneNumber2 = new PhoneNumbers();
                    if (!string.IsNullOrEmpty(model.ApplicantPhoneNumber2))
                    {
                        phoneNumber2.InsertDate = DateTime.Now;
                        phoneNumber2.Number = model.ApplicantPhoneNumber2;
                        phoneNumber2.StatusID = 26;
                        phoneNumber2.UserID = db.Users.Single(x => x.UserName == User.Identity.Name).ID;
                        db.PhoneNumbers.Add(phoneNumber2);
                        db.SaveChanges();
                    }

                    References reference1 = new References();
                    reference1.Company = model.ReferenceCompanyName1;
                    reference1.InsertDate = DateTime.Now;
                    reference1.ModifiedDate = DateTime.Now;
                    reference1.Name = model.ReferenceName1;
                    reference1.Phone = model.ReferencePhoneNumber1;
                    reference1.Relationship = model.ReferenceRelationShip1;
                    reference1.StatusID = 26;
                    reference1.Title = model.ReferenceJobTitle1;
                    db.References.Add(reference1);
                    db.SaveChanges();

                    References reference2 = new References();
                    reference2.Company = model.ReferenceCompanyName2;
                    reference2.InsertDate = DateTime.Now;
                    reference2.ModifiedDate = DateTime.Now;
                    reference2.Name = model.ReferenceName2;
                    reference2.Phone = model.ReferencePhoneNumber2;
                    reference2.Relationship = model.ReferenceRelationShip2;
                    reference2.StatusID = 26;
                    reference2.Title = model.ReferenceJobTitle2;
                    db.References.Add(reference2);
                    db.SaveChanges();

                    References reference3 = new References();
                    reference3.Company = model.ReferenceCompanyName3;
                    reference3.InsertDate = DateTime.Now;
                    reference3.ModifiedDate = DateTime.Now;
                    reference3.Name = model.ReferenceName3;
                    reference3.Phone = model.ReferencePhoneNumber3;
                    reference3.Relationship = model.ReferenceRelationShip3;
                    reference3.StatusID = 26;
                    reference3.Title = model.ReferenceJobTitle3;
                    db.References.Add(reference3);
                    db.SaveChanges();

                    Applications application = new Applications();
                    application.UserID = db.Users.Single(x => x.UserName == User.Identity.Name).ID;
                    application.JobPostingID = (int)jobPostingID;
                    application.AddressID = tempAddress.ID;
                    application.PhoneNumberID1 = phoneNumber1.ID;
                    if (!string.IsNullOrEmpty(model.ApplicantPhoneNumber2))
                        application.PhoneNumberID2 = phoneNumber2.ID;
                    application.InsertDate = DateTime.Now;
                    application.ModifiedDate = DateTime.Now;
                    application.StatusID = 23;
                    application.ReferenceID1 = reference1.ID;
                    application.ReferenceID2 = reference2.ID;
                    application.ReferenceID3 = reference3.ID;
                    application.Salary = model.DesiredSalary;
                    db.Applications.Add(application);
                    
                    db.SaveChanges();
                    appID = application.ID;
                }
                catch (Exception ex)
                {

                }
            }
            return appID;
        }
        private bool UpdateBasicApplication(BasicApplicationModel model, int? BasicApplicationID)
        {
            bool Return = false;

            try
            {
                BasicApplicationInfo basApplication = db.BasicApplicationInfo.Single(x => x.ID == BasicApplicationID);

                basApplication.EmploymentHistory.Name = model.EmployerName1;
                basApplication.EmploymentHistory.Position = model.Position1;
                basApplication.EmploymentHistory.StartDate = model.StartDate1;
                basApplication.EmploymentHistory.EndDate = model.EndDate1;
                basApplication.EmploymentHistory.Address1 = model.Address11;
                basApplication.EmploymentHistory.Address2 = model.Address21;
                basApplication.EmploymentHistory.City = model.City1;
                basApplication.EmploymentHistory.State = model.State1;
                basApplication.EmploymentHistory.Zip = model.Zip1;
                basApplication.EmploymentHistory.ManagerName = model.ManagerName1;
                basApplication.EmploymentHistory.Phone = model.ManagerPhone1;
                basApplication.EmploymentHistory.StartSalary = model.StartSalary1;
                basApplication.EmploymentHistory.EndSalary = model.EndSalary1;
                basApplication.EmploymentHistory.ReasonForLeaving = model.ReasonForLeaving1;
                basApplication.EmploymentHistory.Responsibilities = model.Responsibilities1;
                basApplication.EmploymentHistory.MayWeContact = Convert.ToByte(model.MayWeContact1);
                basApplication.EmploymentHistory.ModifiedDate = DateTime.Now;


                basApplication.Education.SchoolName = model.SchoolName1;
                basApplication.Education.Address1 = model.SchoolAddress11;
                basApplication.Education.Address2 = model.SchoolAddress21;
                basApplication.Education.City = model.SchoolCity1;
                basApplication.Education.State = model.SchoolState1;
                basApplication.Education.Zip = model.SchoolZip1;
                basApplication.Education.YearStart = model.YearStart1;
                basApplication.Education.YearEnd = model.YearEnd1;
                basApplication.Education.Graduated = Convert.ToByte(model.Graduated1);
                basApplication.Education.ModifiedDate = DateTime.Now;


                basApplication.AltName = model.AltName;
                basApplication.Days = Convert.ToByte(model.Days);
                basApplication.Evenings = Convert.ToByte(model.Evenings);
                basApplication.FullTime = Convert.ToByte(model.FullTime);
                basApplication.MonStart = model.MonStart;
                basApplication.MonStop = model.MonStop;
                basApplication.TuesStart = model.TuesStart;
                basApplication.TuesStop = model.TuesStop;
                basApplication.WedStart = model.WedStart;
                basApplication.WedStop = model.WedStop;
                basApplication.ThurStart = model.ThurStart;
                basApplication.ThurStop = model.ThurStop;
                basApplication.FriStart = model.FriStart;
                basApplication.FriStop = model.FriStop;
                basApplication.SatStart = model.SatStart;
                basApplication.SatStop = model.SatStop;
                basApplication.SunStart = model.SunStart;
                basApplication.SunStop = model.SunStop;
                basApplication.SSN = model.SSN;
                basApplication.ModifiedDate = DateTime.Now;
                db.SaveChanges();
                Return = true;
            }
            catch (Exception ex)
            {
            }
            return Return;

        }
        
        private BasicApplicationModel BasicApplicationInfoToApplicationModel(BasicApplicationInfo Bai)
        {
            BasicApplicationModel Bam = new BasicApplicationModel();
            Bam.AltName = Bai.AltName;
            Bam.Days = Convert.ToBoolean(Bai.Days);
            Bam.Evenings = Convert.ToBoolean(Bai.Evenings);
            Bam.FullTime = Convert.ToBoolean(Bai.FullTime);
            Bam.MonStart = Bai.MonStart;
            Bam.MonStop = Bai.MonStop;
            Bam.TuesStart = Bai.TuesStart;
            Bam.TuesStop = Bai.TuesStop;
            Bam.WedStart = Bai.WedStart;
            Bam.WedStop = Bai.WedStop;
            Bam.ThurStart = Bai.ThurStart;
            Bam.ThurStop = Bai.ThurStop;
            Bam.FriStart = Bai.FriStart;
            Bam.FriStop = Bai.FriStop;
            Bam.SatStart = Bai.SatStart;
            Bam.SatStop = Bai.SatStop;
            Bam.SunStart = Bai.SunStart;
            Bam.SunStop = Bai.SunStop;
            Bam.SSN = Bai.SSN;
            Bam.EmployerName1 = Bai.EmploymentHistory.Name;
            Bam.Position1 = Bai.EmploymentHistory.Position;
            Bam.StartDate1 = Bai.EmploymentHistory.StartDate;
            Bam.EndDate1 = Bai.EmploymentHistory.EndDate;
            Bam.Address11 = Bai.EmploymentHistory.Address1;
            Bam.Address21 = Bai.EmploymentHistory.Address2;
            Bam.City1 = Bai.EmploymentHistory.City;
            Bam.State1 = Bai.EmploymentHistory.State;
            Bam.Zip1 = Bai.EmploymentHistory.Zip;
            Bam.ManagerName1 = Bai.EmploymentHistory.ManagerName;
            Bam.ManagerPhone1 = Bai.EmploymentHistory.Phone;
            Bam.StartSalary1 = Bai.EmploymentHistory.StartSalary;
            Bam.EndSalary1 = Bai.EmploymentHistory.EndSalary;
            Bam.ReasonForLeaving1 = Bai.EmploymentHistory.ReasonForLeaving;
            Bam.Responsibilities1 = Bai.EmploymentHistory.Responsibilities;
            Bam.MayWeContact1 = Convert.ToBoolean(Bai.EmploymentHistory.MayWeContact);
            Bam.SchoolName1 = Bai.Education.SchoolName;
            Bam.SchoolAddress11 = Bai.Education.Address1;
            Bam.SchoolAddress21 = Bai.Education.Address2;
            Bam.SchoolCity1 = Bai.Education.City;
            Bam.SchoolState1 = Bai.Education.State;
            Bam.SchoolZip1 = Bai.Education.Zip;
            Bam.YearStart1 = Bai.Education.YearStart;
            Bam.YearEnd1 = Bai.Education.YearEnd;
            Bam.Graduated1 = Convert.ToBoolean(Bai.Education.Graduated);
            return Bam;
        }
        
        private DetailedApplicationModel DetailedApplicationInfoToDetailedApplicationModel(Applications Dai)
        {
            DetailedApplicationModel Dam = new DetailedApplicationModel();

            Dam.Address1 = Dai.Users.Addresses.Single(x => x.UserID == Dai.UserID).Address1;
            Dam.Address2 = Dai.Users.Addresses.Single(x => x.UserID == Dai.UserID).Address2;
            Dam.ApplicantPhoneNumber1 = Dai.PhoneNumbers.Number;
            Dam.ApplicantPhoneNumber2 = Dai.PhoneNumbers1.Number;
            Dam.City = Dai.Users.Addresses.Single(x => x.UserID == Dai.UserID).City;
            Dam.DesiredSalary = Dai.Salary;
            Dam.ReferenceCompanyName1 = Dai.References.Company;
            Dam.ReferenceCompanyName2 = Dai.References1.Company;
            Dam.ReferenceCompanyName3 = Dai.References2.Company;
            Dam.ReferenceJobTitle1 = Dai.References.Title;
            Dam.ReferenceJobTitle2 = Dai.References1.Title;
            Dam.ReferenceJobTitle3 = Dai.References2.Title;
            Dam.ReferenceName1 = Dai.References.Name;
            Dam.ReferenceName2 = Dai.References1.Name;
            Dam.ReferenceName3 = Dai.References2.Name;
            Dam.ReferencePhoneNumber1 = Dai.References.Phone;
            Dam.ReferencePhoneNumber2 = Dai.References1.Phone;
            Dam.ReferencePhoneNumber3 = Dai.References2.Phone;
            Dam.State = Dai.Users.Addresses.Single(x => x.UserID == Dai.UserID).City;
            Dam.Zip = Dai.Users.Addresses.Single(x => x.UserID == Dai.UserID).Zip;

            return Dam;
        }

        #endregion
    }
}
